<?php

App::import('Sanitize');
class MasterlistsController extends AppController {

	var $name = 'Masterlists';
    var $paginate = array('limit' => 100);	
    var $helpers = array('Csv');

	
	function index2() {
		$this->Masterlist->recursive = 0;
		$this->set('masterlists', $this->paginate());
	}
	
	function newmasterlist(){
		if (!empty($this->data)) {
			mysql_query("TRUNCATE TABLE masterlists");
			$this->data = Sanitize::clean($this->data, array('encode' => false)); //clean the array			
	    	$daten = $this->data['Masterlist']['macs'];
	    	$rows = explode("\\n",$daten);
	    	$i = 0;
			foreach($rows as $mac){
				mysql_query("INSERT INTO `mmst_helper`.`masterlists` (`id`, `mac`, `date`) VALUES (NULL, '$mac', NOW());");
			}
			$this->redirect(array('action' => 'index2'));
		}	
	}
	
	function clear(){
		mysql_query("TRUNCATE TABLE masterlists");
		$this->Session->setFlash(__('Masterlist cleared', true));
		$this->redirect(array('action' => 'index2'));
	}
	
	
	function validatemacs(){
		$macdelete = array();
		$i = 0;
		$result = mysql_query("SELECT * FROM masterlists LIMIT 10000");
		WHILE($row = mysql_fetch_array($result))
		  {
			$id = $row["id"];
		  	$mac = $row["mac"];
		  	$partstatus = $this->requestAction(array('controller' => 'oracledmps', 'action' => 'getModemStatus'), array('pass' => array($mac)));
		  	if($partstatus != "In Use"){
				mysql_query("DELETE FROM masterlists WHERE id = '$id' AND mac = '$mac' LIMIT 1");
				$macdelete[$i]['mac'] = $mac;
				$macdelete[$i]['partstatus'] = $partstatus;
				$i++;
			}
		  }
		$this->set(compact('macdelete'));
	}
	
	function removemac() {
		if (!empty($this->data)) {
			$mac = $this->data['Masterlist']['mac'];
			$result = mysql_query("SELECT id FROM masterlists WHERE mac = '$mac'");
			$id = mysql_fetch_object($result)->id;
			if ($this->Masterlist->delete($id)) {
				$this->Session->setFlash(__('Masterlist deleted', true));
				$this->redirect(array('action'=>'index2'));
			}
		}
	}
	
	
	function next500(){
		$this->layout = null;
        $this->autoLayout = false;
        $result = mysql_query("SELECT mac FROM masterlists ORDER BY id ASC LIMIT 500");	
        mysql_query("DELETE FROM masterlists ORDER BY id ASC LIMIT 500");	
        $this->set(compact('result'));
	}
	
	function next750(){
		$this->layout = null;
        $this->autoLayout = false;
        $result = mysql_query("SELECT mac FROM masterlists ORDER BY id ASC LIMIT 750");	
        mysql_query("DELETE FROM masterlists ORDER BY id ASC LIMIT 750");	
        $this->set(compact('result'));
	}
	
	function next1000(){
		$this->layout = null;
        $this->autoLayout = false;
        $result = mysql_query("SELECT mac FROM masterlists ORDER BY id ASC LIMIT 1000");	
        mysql_query("DELETE FROM masterlists ORDER BY id ASC LIMIT 1000");	
        $this->set(compact('result'));
	}
	
	
	
	function index() {
		$this->Masterlist->recursive = 0;
		$this->set('masterlists', $this->paginate());
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid masterlist', true));
			$this->redirect(array('action' => 'index2'));
		}
		$this->set('masterlist', $this->Masterlist->read(null, $id));
	}

	function add() {
		if (!empty($this->data)) {
			$this->Masterlist->create();
			if ($this->Masterlist->save($this->data)) {
				$this->Session->setFlash(__('The masterlist has been saved', true));
				$this->redirect(array('action' => 'index2'));
			} else {
				$this->Session->setFlash(__('The masterlist could not be saved. Please, try again.', true));
			}
		}
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid masterlist', true));
			$this->redirect(array('action' => 'index2'));
		}
		if (!empty($this->data)) {
			if ($this->Masterlist->save($this->data)) {
				$this->Session->setFlash(__('The masterlist has been saved', true));
				$this->redirect(array('action' => 'index2'));
			} else {
				$this->Session->setFlash(__('The masterlist could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Masterlist->read(null, $id);
		}
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for masterlist', true));
			$this->redirect(array('action'=>'index2'));
		}
		if ($this->Masterlist->delete($id)) {
			$this->Session->setFlash(__('Masterlist deleted', true));
			$this->redirect(array('action'=>'index2'));
		}
		$this->Session->setFlash(__('Masterlist was not deleted', true));
		$this->redirect(array('action' => 'index2'));
	}
}
?>